package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.req.IdParam;
import com.example.common.resp.Result;
import com.example.springboot.entity.MenuEntity;
import com.example.springboot.mapper.MenuMapper;
import com.example.springboot.model.MenuSaveParam;
import com.example.springboot.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 路由表 前端控制器
 * </p>
 *
 * @author he
 * @since 2023-11-14
 */
@Tag(name = "二级路由")
@RestController
@RequestMapping("/menuEntity")
public class MenuController {

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    MenuService menuService;

    @Operation(summary = "是否存在")
    @PostMapping("/exist")
    public Integer exist(@RequestBody @Validated IdParam param) {
        QueryWrapper<MenuEntity> menuEntityQueryWrapper = new QueryWrapper<>();
        menuEntityQueryWrapper.eq("id", param.getId());
        if (menuService.count(menuEntityQueryWrapper) == 0) {
            return 0;
        }
        return 1;
    }

    @Operation(summary = "路由列表")
    @GetMapping("/list")
    public Result<List<MenuEntity>> list() {
        return Result.success(menuService.list());
    }

    @Operation(summary = "根据id查找")
    @PostMapping("/detail")
    public Result<MenuEntity> detail(@RequestBody @Validated IdParam param) {
        //ID是否存在
        if (exist(param) == 0) {
            return Result.failure("路由不存在");
        }
        //查找
        return Result.success(menuService.getById(param.getId()));
    }

    @Operation(summary = "保存或修改")
    @PostMapping("/save")
    public Result save(@RequestBody @Validated MenuSaveParam param) {
        MenuEntity menuEntity = new MenuEntity();
        BeanUtils.copyProperties(param, menuEntity);
        if (menuEntity.getId() == 0) {
            menuEntity.setId(null);
            menuEntity.setCreateTime(new Date());
            menuEntity.setUpdateTime(new Date());
            if (menuService.save(menuEntity)) {
                return Result.success();
            }
            return Result.failure("保存失败，请稍后重试");
        } else {
            menuEntity.setUpdateTime(new Date());
            if (menuService.updateById(menuEntity)) {
                return Result.success();
            }
            return Result.failure("修改失败，请稍后重试");
        }
    }

    @Operation(summary = "根据id删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Validated IdParam param) {
        //ID是否存在
        if (exist(param) == 0) {
            return Result.failure("路由不存在");
        }
        //删除
        if (menuService.removeById(param.getId())) {
            return Result.success();
        } else {
            return Result.failure("删除失败，请稍后重试");
        }
    }

}
